﻿using System;
using System.IO;
using System.Diagnostics;
using System.Text;

namespace WPFMetronom
{
	static class Logger
	{
		#region Declarations

		private static string msPathToLog = "";
		private static StreamWriter mswStreamWriter;

		#endregion

		#region Constructors

		static Logger ( )
		{
			msPathToLog = Directory.GetCurrentDirectory ( );
			mswStreamWriter = new StreamWriter ( Path.Combine ( msPathToLog, DateTime.Now.DayOfWeek.ToString ( ) ), true );
		}

		#endregion

		#region Public Methods

		public static void Log ( Exception oException )
		{
			StringBuilder sbTemp = new StringBuilder ( );
			sbTemp.AppendLine ( DateTime.Now.ToString ( ) + " " + oException.Message );

			StackTrace stTrace = new StackTrace ( oException, true );
			foreach ( StackFrame stackFrame in stTrace.GetFrames ( ) )
			{
				sbTemp.AppendLine ( Path.GetFileName ( stackFrame.GetFileName ( ) ) + " " + stackFrame.GetFileLineNumber ( ) + " " + stackFrame.GetFileColumnNumber ( ) + " " + stackFrame.GetMethod ( ) );
			}
			mswStreamWriter.WriteLine ( sbTemp );
			mswStreamWriter.Flush ( );
			//streamWriter.Close();
		}

		public static void Log ( string sMessage )
		{
			StringBuilder sbTemp = new StringBuilder ( );
			sbTemp.AppendLine ( DateTime.Now.ToString ( ) + " " + sMessage );

			mswStreamWriter.WriteLine ( sbTemp );
			mswStreamWriter.Flush ( );
		}

		#endregion
	}
}
